package tfmcbs.diagram.edit.parts;

import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import tfmcbs.diagram.part.ComponentBasedSystemVisualIDRegistry;

/**
 * @generated
 */
public class ComponentBasedSystemEditPartFactory implements EditPartFactory {

	/**
	 * @generated
	 */
	public EditPart createEditPart(EditPart context, Object model) {
		if (model instanceof View) {
			View view = (View) model;
			switch (ComponentBasedSystemVisualIDRegistry.getVisualID(view)) {

			case ModelEditPart.VISUAL_ID:
				return new ModelEditPart(view);

			case PrimitiveComponentEditPart.VISUAL_ID:
				return new PrimitiveComponentEditPart(view);

			case PrimitiveComponentNameEditPart.VISUAL_ID:
				return new PrimitiveComponentNameEditPart(view);

			case PrimitiveComponentStereotypeEditPart.VISUAL_ID:
				return new PrimitiveComponentStereotypeEditPart(view);

			case CompositeComponentEditPart.VISUAL_ID:
				return new CompositeComponentEditPart(view);

			case CompositeComponentNameEditPart.VISUAL_ID:
				return new CompositeComponentNameEditPart(view);

			case CompositeComponentStereotypeEditPart.VISUAL_ID:
				return new CompositeComponentStereotypeEditPart(view);

			case InterfaceEditPart.VISUAL_ID:
				return new InterfaceEditPart(view);

			case InterfaceNameEditPart.VISUAL_ID:
				return new InterfaceNameEditPart(view);

			case ArtifactEditPart.VISUAL_ID:
				return new ArtifactEditPart(view);

			case ArtifactNameEditPart.VISUAL_ID:
				return new ArtifactNameEditPart(view);

			case ArtifactStereotypeEditPart.VISUAL_ID:
				return new ArtifactStereotypeEditPart(view);

			case PrimitiveComponent2EditPart.VISUAL_ID:
				return new PrimitiveComponent2EditPart(view);

			case PrimitiveComponentName2EditPart.VISUAL_ID:
				return new PrimitiveComponentName2EditPart(view);

			case PrimitiveComponentStereotype2EditPart.VISUAL_ID:
				return new PrimitiveComponentStereotype2EditPart(view);

			case CompositeComponent2EditPart.VISUAL_ID:
				return new CompositeComponent2EditPart(view);

			case CompositeComponentName2EditPart.VISUAL_ID:
				return new CompositeComponentName2EditPart(view);

			case CompositeComponentStereotype2EditPart.VISUAL_ID:
				return new CompositeComponentStereotype2EditPart(view);

			case Interface2EditPart.VISUAL_ID:
				return new Interface2EditPart(view);

			case InterfaceName2EditPart.VISUAL_ID:
				return new InterfaceName2EditPart(view);

			case Artifact2EditPart.VISUAL_ID:
				return new Artifact2EditPart(view);

			case ArtifactName2EditPart.VISUAL_ID:
				return new ArtifactName2EditPart(view);

			case ArtifactStereotype2EditPart.VISUAL_ID:
				return new ArtifactStereotype2EditPart(view);

			case ProvidingPortEditPart.VISUAL_ID:
				return new ProvidingPortEditPart(view);

			case ProvidingPortNameEditPart.VISUAL_ID:
				return new ProvidingPortNameEditPart(view);

			case WrappingLabelEditPart.VISUAL_ID:
				return new WrappingLabelEditPart(view);

			case RequiringPortEditPart.VISUAL_ID:
				return new RequiringPortEditPart(view);

			case RequiringPortNameEditPart.VISUAL_ID:
				return new RequiringPortNameEditPart(view);

			case WrappingLabel2EditPart.VISUAL_ID:
				return new WrappingLabel2EditPart(view);

			case CompositeComponentCompositeComponentCompartmentEditPart.VISUAL_ID:
				return new CompositeComponentCompositeComponentCompartmentEditPart(
						view);

			case CompositeComponentCompositeComponentCompartment2EditPart.VISUAL_ID:
				return new CompositeComponentCompositeComponentCompartment2EditPart(
						view);

			case ConnectionEditPart.VISUAL_ID:
				return new ConnectionEditPart(view);

			case ConnectionNameEditPart.VISUAL_ID:
				return new ConnectionNameEditPart(view);

			case DependencyEditPart.VISUAL_ID:
				return new DependencyEditPart(view);

			case DependencyNameEditPart.VISUAL_ID:
				return new DependencyNameEditPart(view);

			case DependencyStereotypeEditPart.VISUAL_ID:
				return new DependencyStereotypeEditPart(view);

			case ManifestationEditPart.VISUAL_ID:
				return new ManifestationEditPart(view);

			case WrappingLabel3EditPart.VISUAL_ID:
				return new WrappingLabel3EditPart(view);

			case PrimitiveUsageEditPart.VISUAL_ID:
				return new PrimitiveUsageEditPart(view);

			case PrimitiveUsageNameEditPart.VISUAL_ID:
				return new PrimitiveUsageNameEditPart(view);

			case PortUsageEditPart.VISUAL_ID:
				return new PortUsageEditPart(view);

			case PortUsageNameEditPart.VISUAL_ID:
				return new PortUsageNameEditPart(view);

			case PrimitiveRealizationEditPart.VISUAL_ID:
				return new PrimitiveRealizationEditPart(view);

			case PrimitiveRealizationNameEditPart.VISUAL_ID:
				return new PrimitiveRealizationNameEditPart(view);

			case PortRealizationEditPart.VISUAL_ID:
				return new PortRealizationEditPart(view);

			case PortRealizationNameEditPart.VISUAL_ID:
				return new PortRealizationNameEditPart(view);

			case ProvidingPortDelegateEditPart.VISUAL_ID:
				return new ProvidingPortDelegateEditPart(view);

			case WrappingLabel4EditPart.VISUAL_ID:
				return new WrappingLabel4EditPart(view);

			case RequiringPortDelegateEditPart.VISUAL_ID:
				return new RequiringPortDelegateEditPart(view);

			case WrappingLabel5EditPart.VISUAL_ID:
				return new WrappingLabel5EditPart(view);

			case EntityExtendsEditPart.VISUAL_ID:
				return new EntityExtendsEditPart(view);

			}
		}
		return createUnrecognizedEditPart(context, model);
	}

	/**
	 * @generated
	 */
	private EditPart createUnrecognizedEditPart(EditPart context, Object model) {
		// Handle creation of unrecognized child node EditParts here
		return null;
	}

	/**
	 * @generated
	 */
	public static CellEditorLocator getTextCellEditorLocator(
			ITextAwareEditPart source) {
		if (source.getFigure() instanceof WrappingLabel)
			return new TextCellEditorLocator((WrappingLabel) source.getFigure());
		else {
			return new LabelCellEditorLocator((Label) source.getFigure());
		}
	}

	/**
	 * @generated
	 */
	static private class TextCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private WrappingLabel wrapLabel;

		/**
		 * @generated
		 */
		public TextCellEditorLocator(WrappingLabel wrapLabel) {
			this.wrapLabel = wrapLabel;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getWrapLabel() {
			return wrapLabel;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getWrapLabel().getTextBounds().getCopy();
			getWrapLabel().translateToAbsolute(rect);
			if (!text.getFont().isDisposed()) {
				if (getWrapLabel().isTextWrapOn()
						&& getWrapLabel().getText().length() > 0) {
					rect.setSize(new Dimension(text.computeSize(rect.width,
							SWT.DEFAULT)));
				} else {
					int avr = FigureUtilities.getFontMetrics(text.getFont())
							.getAverageCharWidth();
					rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
							SWT.DEFAULT)).expand(avr * 2, 0));
				}
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}

	/**
	 * @generated
	 */
	private static class LabelCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private Label label;

		/**
		 * @generated
		 */
		public LabelCellEditorLocator(Label label) {
			this.label = label;
		}

		/**
		 * @generated
		 */
		public Label getLabel() {
			return label;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getLabel().getTextBounds().getCopy();
			getLabel().translateToAbsolute(rect);
			if (!text.getFont().isDisposed()) {
				int avr = FigureUtilities.getFontMetrics(text.getFont())
						.getAverageCharWidth();
				rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
						SWT.DEFAULT)).expand(avr * 2, 0));
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}
}
